package com.technology.education.ui.teacher.popularize

import androidx.core.os.bundleOf
import com.angcyo.dsladapter.*
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.AppData
import com.technology.education.data.model.Record
import com.technology.education.dslitem.ItemSingleText
import com.technology.education.ext.*
import me.hgj.jetpackmvvm.ext.parseState

/**
 * 辅导老师推广中心
 * 积分只能进行提现操作
 */
class PopularizeFragment : BaseRecyclerTopBarFragment<PopularizeViewModel>() {
    override fun initViews() {
        setTitle("推广中心")
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            updateOrInsertHeaderItem<DslAdapterItem>("code", 0) {
                it.apply {
                    itemLayoutId = R.layout.item_invitated_code
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemTag = "code"
                        itemHolder.tv(R.id.tv_code)?.text =
                            getAppViewModel().userInfo.value?.inviteCode
                        itemHolder.click(R.id.iv_copy) {
                            (getAppViewModel().userInfo.value?.inviteCode ?: "").copyToClip(
                                requireContext()
                            )
                            toast("邀请码已复制到剪切板")
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<DslAdapterItem>("time", 1) {
                it.apply {
                    itemLayoutId = R.layout.item_mine_time_remain
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemTag = "time"
                        itemHolder.tv(R.id.tv_time_remain)?.text =
                            getAppViewModel().userInfo.value?.balancePoints.toString()
                        itemHolder.tv(R.id.tv_time_record)?.text = "积分规则"
                        itemHolder.tv(R.id.textview)?.text = "我的积分"
                        itemHolder.click(R.id.tv_time_record) {
                            toWebActivity("积分规则", AppData.SCORE_PROTOCOL)
                        }
                        itemHolder.tv(R.id.tv_pay)?.text = "兑换现金"
                        itemHolder.click(R.id.tv_pay) {
                            navigation(
                                requireView(),
                                R.id.action_popularize_to_withdraw,
                                bundleOf("type" to 1)
                            )
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<ItemSingleText>("info", 2) {
                it.apply {
                    title = "获得时长已自动添加至时长余额"
                    style.titleTextColor = R.color.red
                    style.titleTextSize = 14f
                    style.topPadding = 10
                }
            }

            updateOrInsertHeaderItem<DslAdapterItem>("menu", 3) {
                it.apply {
                    itemLayoutId = R.layout.item_popularize_menu
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.click(R.id.tv_mine) { view ->
                            ///我的推广
                            navigation(view, R.id.action_popularize_to_record)
                        }
                        itemHolder.click(R.id.tv_invite) {it->
                            //todo:立即推广

                        }
                        itemHolder.click(R.id.tv_record) { it ->
                            //提现记录
                            navigation(it, R.id.action_popularize_to_withdraw_record)
                        }
                    }
                }
            }

            updateOrInsertHeaderItem<ItemSingleText>("title", 4) {
                it.apply {
                    title = "奖励记录"
                    style.isBold = true
                    style.bottomPadding = 0
                }
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(
                        it.list.toDslItemList(R.layout.item_score_record) {
                            itemBindOverride = { itemHolder, _, _, _ ->
                                itemData?.let { data ->
                                    if (data is Record) {
                                        itemHolder.tv(R.id.tv_title)?.text = data.title
                                        itemHolder.tv(R.id.tv_score)?.text = data.valueString
                                        itemHolder.tv(R.id.tv_date)?.text = data.createDate
                                    }
                                }
                            }
                        },
                        mViewModel.pageIndex
                    )
                }
            }, onError = {
                loadError()
            })
        }

        getAppViewModel().userInfo.observe(this) {
            renderAdapter {
                updateAllItem()
            }
        }
    }
}